//
//  EmotionView.m
//  SinaWebo
//
//  Created by wwj on 16/5/16.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "EmotionView.h"
#import "EmotionTool.h"
#import "EmotionModel.h"
#import "EmotionPageModel.h"
#import "EmotionViewCell.h"
#import "Hairline.h"
#import <Masonry.h>

#define kToolBarH 44

@interface EmotionView ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>
@property (weak, nonatomic) UIScrollView *toolBar;
@property (weak, nonatomic) UIScrollView *scrollerView;
@property (weak, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout *flowLayout;
@property (strong, nonatomic)  NSArray *emotionArray;

@property (weak, nonatomic) UIButton *selectBtn;

@property (strong, nonatomic) NSMutableArray<NSNumber *> *pageArray;
@property (strong, nonatomic) NSMutableArray *toobArray;
@end

@implementation EmotionView
static NSString *const ID = @"EmotionViewCell";

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (NSArray *)emotionArray{
    if (_emotionArray == nil) {
        _emotionArray = [NSArray array];
    }
    return _emotionArray;
}

- (void)initView{
    [self addToolBarView];
    [self addCollectionView];
    
    [self getEmotionData];
}

- (void)addToolBarView{
    UIScrollView *toolBar = [[UIScrollView alloc] init];
    toolBar.backgroundColor = MyColor(239, 239, 239);
    [self addSubview:toolBar];
    self.toolBar = toolBar;
    [self.toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.bottom.mas_equalTo(self.mas_bottom);
        make.height.mas_equalTo(kToolBarH);
    }];
    
}

- (void)addCollectionView{
    CGFloat itemW = kScreenW / 7;
    CGFloat itemH = 48;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.itemSize = CGSizeMake(itemW, itemH);
    self.flowLayout = flowLayout;
    
    CGRect frame = CGRectMake(0, 0, kScreenW, itemH * 3);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
    collectionView.backgroundColor = MyColor(250, 250, 250);
    collectionView.pagingEnabled = YES;
    collectionView.showsHorizontalScrollIndicator = NO;
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([EmotionViewCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.top.mas_equalTo(self.mas_top);
        make.bottom.mas_equalTo(self.toolBar.mas_top);
    }];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat margin = (self.collectionView.bounds.size.height - self.flowLayout.itemSize.height * 3);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, margin, 0);
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.emotionArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    EmotionPageModel *pageModel = self.emotionArray[section];
    return pageModel.emoticons.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EmotionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    EmotionPageModel *pageModel = self.emotionArray[indexPath.section];
    EmotionModel *emotionModel = pageModel.emoticons[indexPath.item];
    cell.emotion = emotionModel;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    EmotionPageModel *pageModel = self.emotionArray[indexPath.section];
    EmotionModel *emotionModel = pageModel.emoticons[indexPath.item];
    emotionModel.time++;
    [[EmotionTool sharedEmotion] addEmotion:emotionModel];
    if (_block) {
        _block(emotionModel);
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger page = scrollView.contentOffset.x / scrollView.width;
    NSInteger pageCount = 0;
    for (NSInteger index = self.pageArray.count - 1; index>0; index--) {
        if (page >= [self.pageArray[index] integerValue]) {
            pageCount = index;
            break;
        }
    }
    
    [self.toobArray enumerateObjectsUsingBlock:^(UIButton *btn, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == pageCount) {
            [self changeToolBar:btn];
        }
    }];
}

- (void)getEmotionData{
    self.emotionArray = [EmotionTool sharedEmotion].emotionPages;
    [self setupTooBar];
    [self.collectionView reloadData];
    
    
}

- (void)setupTooBar{
    CGFloat with = self.collectionView.width / self.emotionArray.count;
    CGFloat height = kToolBarH;
    self.pageArray = [NSMutableArray array];
    self.toobArray = [NSMutableArray array];
    
    NSInteger page = 0;
    for (NSInteger index = 0; index < self.emotionArray.count; index++) {
        [self.pageArray addObject:@(page)];
        EmotionPageModel *group = self.emotionArray[index];
        UIButton *tooBarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tooBarBtn.frame = CGRectMake(with * index, 0, with, height);
        tooBarBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [tooBarBtn setTitle:group.group_name_cn forState:UIControlStateNormal];
        [tooBarBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [tooBarBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateSelected];
        [tooBarBtn setBackgroundColor:MyColor(239, 239, 239)];
        tooBarBtn.tag = index;
        [tooBarBtn addTarget:self action:@selector(onClickTooBar:) forControlEvents:UIControlEventTouchUpInside];
        tooBarBtn.backgroundColor = [UIColor whiteColor];
        
        NSInteger count = group.emoticons.count / 21;
        page+= count;
        
        [self.toolBar addSubview:tooBarBtn];
        [self.toobArray addObject:tooBarBtn];
    }
    self.toolBar.contentSize = CGSizeMake(self.emotionArray.count * with, 0);
    [self onClickTooBar:self.toobArray.firstObject];
}

- (void)onClickTooBar:(UIButton *)btn{
    self.selectBtn.selected = NO;
    self.selectBtn.backgroundColor = MyColor(239, 239, 239);
    btn.selected = YES;
    [btn setBackgroundColor:MyColor(215, 215, 215)];
    self.selectBtn = btn;
    NSInteger page = [self.pageArray[btn.tag] integerValue];
    CGRect frame = CGRectMake(_collectionView.width * page, 0, _collectionView.width, _collectionView.height);
    [self.collectionView scrollRectToVisible:frame animated:NO];
}

- (void)changeToolBar:(UIButton *)btn{
    self.selectBtn.selected = NO;
    self.selectBtn.backgroundColor = MyColor(239, 239, 239);
    btn.selected = YES;
    [btn setBackgroundColor:MyColor(215, 215, 215)];
    self.selectBtn = btn;
}

@end
